﻿@using System.Activities.Expressions
@using System.Drawing
@using JSTree.Sample.Models
@model IEnumerable<Node>

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@functions
{
    private string str = "";
    public string CreateNode(Node node)
    {
        var selNodes = Model.Where(x => x.ParentId == node.Id).ToList();
        if (selNodes.Count() == 0)
        {
            return "";
        }

        foreach (var selNode in selNodes)
        {
            Node childNode = new Node();
            childNode.Id = selNode.Id;
            str += "<li id='node_" + selNode.Id + "' rel='" + selNode.Type + "'><a href='#'>" + selNode.Title + "</a><ul>";
            CreateNode(childNode);
            str += "</ul></li>";
        }
        return str;
    }
}

<div id="menu">
    <input id="add_folder" type="button" value="add folder"/>
    <input id="add_file" type="button" value="add file"/>
    <input id="rename" type="button" value="rename"/>
    <input id="remove" type="button" value="remove"/>
</div>
<div id="tree">
    <ul>
    @{
        var nodes = Model.Where(x => x.ParentId == null).ToList();
        foreach (var node in nodes)
        {
            Node root = new Node();
            root.Id = node.Id;
            <li id="node_@node.Id" rel="drive">
                <a href="#">@node.Title</a>
                <ul>
                    @Html.Raw(CreateNode(root))
                </ul>
            </li>
            str = "";
        }
    }
    </ul>
</div>